#include "MainWnd.h"
#include "ui_MainWnd.h"

#include "ui/widget/ComCtrlWidget.h"
#include "data/ParseData.h"

#include <QMessageBox>
#include <QInputDialog>

MainWnd::MainWnd(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWnd)
{
    ui->setupUi(this);

    p_parseData = new ParseData();

    p_comCtrlWidget = new ComCtrlWidget();
    connect(p_comCtrlWidget, &ComCtrlWidget::debugMessage, this, &MainWnd::onMessageComming);
    connect(p_comCtrlWidget, &ComCtrlWidget::dataReceived, this, &MainWnd::onDataReceived);
    connect(p_comCtrlWidget, &ComCtrlWidget::openPortSuccess, this, [=](){
        ui->groupControl->setEnabled(true);
    });
    connect(p_comCtrlWidget, &ComCtrlWidget::openPortFailed, this, [=](){
        ui->groupControl->setEnabled(false);
    });
    connect(p_comCtrlWidget, &ComCtrlWidget::closePortSuccess, this, [=](){
        ui->groupControl->setEnabled(false);
    });
    ui->layoutSettings->addWidget(p_comCtrlWidget);

    //..........
    ui->btnHelp->hide();
    ui->groupControl->setEnabled(false);
}

MainWnd::~MainWnd()
{
    delete ui;
}

void MainWnd::onDataReceived(const QByteArray &data)
{
    ui->txtMessage->appendPlainText("RECV: "+data);

    QString txt = p_parseData->parse(data);
    if (!txt.isEmpty()) {
        ui->txtDetail->appendPlainText(txt);
    }
}

void MainWnd::onMessageComming(const QString &text)
{
    ui->txtMessage->appendPlainText(text);
}

void MainWnd::on_btnSend_clicked()
{
    QString text = ui->edtSend->text();
    if (ui->chkSendHexMode->isChecked()) {
        QByteArray ba = QByteArray::fromHex(text.toUtf8());
        if (ba.toHex().toLower() == ui->edtSend->text().toLower()) {
            if (ui->chkSendWithReturnChar->isChecked()) {
                ba.append("\r\n");
                p_comCtrlWidget->sendData(ba);
            } else {
                p_comCtrlWidget->sendData(ba);
            }

        } else {
            QMessageBox::critical(this, "", tr("Input content is not HEX text!"));
        }

    } else {
        if (ui->chkSendWithReturnChar->isChecked()) {
            text.append("\r\n");
        }

        p_comCtrlWidget->sendData(text.toUtf8());
    }
}

void MainWnd::sendBtCmd(const QString &text)
{
    ui->txtMessage->appendPlainText("SEND: " + text);
    p_comCtrlWidget->sendData(QString("%1\r\n").arg(text).toUtf8());
}
//////////////////////////////////////////////////////
void MainWnd::on_btnTest_clicked()
{
    sendBtCmd("AT");
}

void MainWnd::on_btnVersion_clicked()
{
    sendBtCmd("AT+VERSION");
}

void MainWnd::on_btnHelp_clicked()
{
    sendBtCmd("AT+HELP");
}

void MainWnd::on_btnReset_clicked()
{
    sendBtCmd("AT+RESET");
}

void MainWnd::on_btnRestoreFactory_clicked()
{
    sendBtCmd("AT+DEFAULT");
}

void MainWnd::on_btnSleep_clicked()
{
    sendBtCmd("AT+SLEEP");
}

void MainWnd::on_btnAddressGet_clicked()
{
    sendBtCmd("AT+LADDR");
}

void MainWnd::on_btnNameGet_clicked()
{
    sendBtCmd("AT+NAME");
}

void MainWnd::on_btnNameSet_clicked()
{
    QString text = QInputDialog::getText(this, "", tr("SET DEVICE NAME:"), QLineEdit::Normal, "MLT-BT05");
    if (!text.isEmpty()) {
        sendBtCmd(QString("AT+NAME%1").arg(text));
    }
}

void MainWnd::on_btnPinGet_clicked()
{
    sendBtCmd("AT+PIN");
}

void MainWnd::on_btnPinSet_clicked()
{
    bool ok;
    int i = QInputDialog::getInt(this, "", tr("SET PIN:"), 123456, 0, 999999, 1, &ok);
    if (ok) {
        sendBtCmd(QString("AT+PIN%1").arg(i));
    }
}

void MainWnd::on_btnAuthGet_clicked()
{
    sendBtCmd("AT+TYPE");
}

void MainWnd::on_btnAuthSet_clicked()
{
    QStringList items = { tr("NONE"), tr("EASY"), tr("PIN"), tr("PIN & BIND") };

    bool ok;
    QString item = QInputDialog::getItem(this, "", tr("AUTH Method:"), items, 0, false, &ok);
    if (ok && !item.isEmpty()) {
        sendBtCmd(QString("AT+TYPE%1").arg(items.indexOf(item)));
    }
}

void MainWnd::on_btnServiceUuidGet_clicked()
{
    sendBtCmd("AT+UUID");
}

void MainWnd::on_btnServiceUuidSet_clicked()
{
    QString text = QInputDialog::getText(this, "", tr("SET Service UUID:"), QLineEdit::Normal, "FFE0");
    if (!text.isEmpty()) {
        sendBtCmd(QString("AT+UUID%1").arg(text));
    }
}

void MainWnd::on_btnCharacteristicGet_clicked()
{
    sendBtCmd("AT+CHAR");
}

void MainWnd::on_btnCharacteristicSet_clicked()
{
    QString text = QInputDialog::getText(this, "", tr("SET Characteristic:"), QLineEdit::Normal, "FFE1");
    if (!text.isEmpty()) {
        sendBtCmd(QString("AT+CHAR%1").arg(text));
    }
}
